package com.chrudgames.juicedietrecipes;


import android.app.ListActivity;
import android.content.Intent;
import android.database.Cursor;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.ListView;
import android.widget.SimpleCursorAdapter;
import android.widget.SimpleCursorAdapter.ViewBinder;

public class JuiceDietRecipesActivity extends ListActivity {

	 SimpleCursorAdapter adapter;
	Cursor c;

	 
	 
	 @Override
	 
	  
	 public void onCreate(Bundle savedInstanceState) {
	        super.onCreate(savedInstanceState);
	        setContentView(R.layout.custom_list_view);
	        

	    	DatabaseHandler db = new DatabaseHandler(this);     
	    	
	    	if(db.getRecipesCount()<39) {
	    	    
	    		db.runQuery("delete from info");
	    		 db.runQuery("DROP TABLE IF EXISTS Info");
	    	db.reCreateDB();	
	

	    	}
	        
	    	
	        db.close();
	        
	        
	        FillScreen();
	        
	     
	    }
	 
	 
		public void onClick(View v) {
	    	switch (v.getId()) {
	    	
	    	case R.id.imageView2:
	    		Intent i5 = new Intent(this, Search.class);
	          	startActivity(i5);
	          	break;
	          	
	    	}
		}
	 

	 
	 @Override
	 public boolean onCreateOptionsMenu(Menu menu){
		 MenuInflater inflater = getMenuInflater();
		 inflater.inflate(R.menu.options, menu);
		 return true;
	 }
	 
	 @Override
	 public boolean onOptionsItemSelected(MenuItem item){
		 switch (item.getItemId()){
		 
         case R.id.search:
        	 Intent i3 = new Intent(this, Search.class);
        	 	startActivity(i3);
        	 return true;
		 
		 case R.id.generald:
	    		Intent i = new Intent(this, General.class);
	          	startActivity(i);
			 return true;
			 
         case R.id.help:
         	Intent i2 = new Intent(this, Help.class);
            	startActivity(i2);
             return true;
             
			 default:
				 return super.onOptionsItemSelected(item);
		 }
	 }
	    


	    
	    protected void onListItemClick(ListView l, View v, int position, long id) {

	    	   //super.onListItemClick(l, v, position, id);
	           //Object o = this.getListAdapter().getItem(position);
	    	
	    	c = ((SimpleCursorAdapter)l.getAdapter()).getCursor();
	           c.moveToPosition(position);

	           final String lookupKey = c.getString(0);
	  	
		   c.close(); 		                	
	           	          
	           Intent i = new Intent(this, details.class);
	           Bundle b1 = new Bundle();
	           

	           b1.putString("lookupKey", lookupKey);

	                   i.putExtras(b1);
	                           
	                   
	                 	startActivity(i);
	        }
		    	
	    	
	    
	    
	    @Override
	    public void onResume()
	    {
	    	 FillScreen(); 
	    	    
	    	   super.onResume();
	    }
	    
	    
	    @Override
	    public void onStop()
	    {
	    	//c.close();
	    	adapter=null; 
	    	    
	        super.onStop();
	    }
	    
	     
	    private void FillScreen() {
	    	
	    	DatabaseHandler db = new DatabaseHandler(this);

	       // db.reCreateDB();
	               
	         String[] fields = new String[]  {"name","ing","favorite","favorite"};
	         int[] views = new int[] {R.id.text1,R.id.text2,R.id.favon,R.id.favoff };
	         Cursor c = (Cursor) db.getAllRecipesCursor();
	        
	        
	        adapter = new SimpleCursorAdapter(
	                 this,
	                 R.layout.custom_row_view,
	                 c, fields, views);
	         
	         adapter.setViewBinder(new ProductViewBinder());

	         setListAdapter(adapter);

	          
	         db.close();
	    	
	    	
	    	
	    }

	    private static class ProductViewBinder implements ViewBinder {

	        public boolean setViewValue(View view, Cursor cursor, int columnIndex) {
	        	
	        	        	
	        	if (view.getId()==R.id.favoff) {
	        		
	        		
	        		if(cursor.getString(6).contentEquals("0")){
	        			
   	        		        			
	        			view.setVisibility(View.VISIBLE);
	        			
	        		}else{
	        			
	        			view.setVisibility(View.GONE);
	        		}
	        		
	        		return true;
	        	
	        	
	        		
	        	}else if (view.getId()==R.id.favon) {
	        		
	        		
	        		if(cursor.getString(6).contentEquals("1")){
	        			
	        			view.setVisibility(View.VISIBLE);
	        			
	        		}else{
	        			
	        			view.setVisibility(View.GONE);
	        		}
	        		
	        		return true;
	            }

	           return false;
	        }
	    }

	    
	    
	    }


    
